import {HxPost} from '../service/http';
import {json2String} from './utils';
import {hxData} from './hx-data';
import {HxTaro} from './hx-taro';

const wxPay = (appId, path, extend, sucTips, sucFun, payWay) => {
    if (extend) {
        uni.requestPayment({
            paySign: extend.sign,
            timeStamp: extend.timeStamp,
            signType: extend.signType,
            package: extend.package,
            nonceStr: extend.nonceStr,
        })
            // @ts-ignore
            .then(() => {
                if (payWay === '0') {
                    return Promise.resolve();
                } else {
                    return uni.showModal({
                        title: '',
                        content: sucTips,
                        showCancel: false,
                        confirmText: '确定'
                    });
                }
            })
            .then(() => {
                sucFun();
            })
            .catch((err) => {
                console.log(err)
                switch (err.errMsg) {
                    case 'requestPayment:fail cancel':
                    case 'navigateToMiniProgram:fail cancel':
                        err = {retMsg: '取消支付'};
                        break;
                }
                uni.hideLoading();
                uni.showToast({title: err.retMsg || '未知错误', icon: 'none', duration: 2 * 1000}).catch();
            })
    } else {
        uni.navigateToMiniProgram({
            appId,
            path,
        })
            // @ts-ignore
            .then(() => {
                if (payWay === '0') {
                    return Promise.resolve();
                } else {
                    return uni.showModal({
                        title: '',
                        content: sucTips,
                        showCancel: false,
                        confirmText: '确定'
                    });
                }
            })
            .then(() => {
                sucFun();
            })
            .catch((err) => {
                console.log(err)
                switch (err.errMsg) {
                    case 'requestPayment:fail cancel':
                    case 'navigateToMiniProgram:fail cancel':
                        err = {retMsg: '取消支付'};
                        break;
                }
                uni.hideLoading();
                uni.showToast({title: err.retMsg || '未知错误', icon: 'none', duration: 2 * 1000}).catch();
            });
    }
};

const take = (list, index) => {
    uni.showToast({title: '正在查询中奖信息...', icon: 'none', duration: 2 * 1000}).catch();
    hxData.set('rewardTake', list[index]);
    return HxPost('rewardTake', {bonusdetailId: [list[index].bonusDetailId]})
        .then((res) => {
            if (res.resultFlag === '2') {
                return Promise.reject('takeChange');
            } else {
                return Promise.resolve(res);
            }
        })
        .catch((err) => {
            if (err === 'takeChange') {
                takeChange();
            } else {
                HxTaro.toast(err.retMsg);
            }
        })
        .finally(() => {
            uni.hideLoading();
        });
};

const takeChange = () => {
    HxPost('rewardTrade', {callBackUrl: '/pages/rewardResult/index'})
        .then((res) => {
            uni.navigateTo({
                url: '/pages/rewardTake/index?url=' + encodeURIComponent(res.transAuthUrl)
            });
        })
        .catch((err) => {
            HxTaro.toast(err.retMsg);
        });
};

const take2 = (rewardTake, params) => {
    uni.showLoading({title: '正在领奖...', mask: true});
    return HxPost('rewardFeed', {callBackInfo: json2String(params)})
        .then(() => {
            return HxPost('rewardTake', {bonusdetailId: [rewardTake.bonusDetailId]});
        })
        .then((res) => {
            if (res.resultFlag === '0') {
                HxTaro.toast(res.resultDesc || '领奖成功');
                return Promise.resolve('');
            } else {
                return Promise.reject({retMsg: res.resultDesc});
            }
        })
        .catch((err) => {
            console.log(err);
            HxTaro.toast(err.retMsg);
            return Promise.reject();
        })
        .finally(() => {
            uni.hideLoading();
        });
};

const wxPayAll = (chargeId, payChannelId = '1') => {
    let param = {
        payChannelId,
        chargeId,
    };
    return HxPost('getPayUrl', param)
        .then((res) => {
            const extend = res.wxPayExtendInfo
            return uni.requestPayment({
                paySign: extend.sign,
                timeStamp: extend.timeStamp,
                signType: extend.signType,
                package: extend.package,
                nonceStr: extend.nonceStr,
            })
        })
}

export const HxPay = {
    wxPay,
    take,
    take2,
    wxPayAll,
};
